<template>
  <div id="ToolLabel" v-if="isNeedLoad" v-show="isToolWebShow">
    <iframe class="IframStyle" ref="toolLabelIframe" sandbox="allow-forms allow-scripts allow-same-origin allow-popups allow-top-navigation" scrolling="no" frameborder="0" src=""></iframe>
  </div>
</template>

<script>
    import BaseComponent from "./BaseComponent";
    import {GetQtController} from "../../QtUtils";
    import {PUBSUB_KEY_TOOLWEBSHOW_CHANGED} from "../../QtBaseController";

    export default {
      extends:BaseComponent,
      name: "ToolLabel",
      data() {
        return {
          unique_name: 'ToolLabel',

          isToolWebShow:false,
          isNeedLoad:false,
          projectName:[],
        }
      },
      methods:{
        onCreated() {
          window.addEventListener("message", this._receiveMessage.bind(this), false);
          this.isNeedLoad = (GetQtController().inParam.toolWebUrl != '');
        },
        onMounted() {
          if(this.isNeedLoad) {
            let strSrc = GetQtController().inParam.toolWebUrl;
            strSrc += '?commonHost=' + GetQtController().inParam.toolServerUrl;
            strSrc += "&isClient=1&isFrame=1&isWebLog=1";
            // strSrc += "&project_name=" + GetQtController().inParam.strProjectName;
            this.$refs.toolLabelIframe.src = strSrc;

            this.$refs.toolLabelIframe.onload = function() {
              console.log('1111111--onload',GetQtController().inParam.toolProjectName)
              this._postMessage('VS_SetProjectName',[GetQtController().inParam.toolProjectName]);
            }.bind(this);
          }
        },
        onDestroyed() {
          window.removeEventListener("message",this._receiveMessage);
        },
        onPubSubMessage(strCode,data) {
          if(strCode == 'VS_SetPointPos' || strCode == 'VS_SetProjectName') {
            this._postMessage(strCode,data);
          }else if(strCode == PUBSUB_KEY_TOOLWEBSHOW_CHANGED) {
            this.isToolWebShow = data;
            GetQtController().Web_ToolWebShow(data);
          }
        },

        _postMessage(strCode,data) {
          if(this.isNeedLoad) {
            let postData = {};
            postData['key'] = 'ToIFrameQtMessage';
            postData['code'] = strCode;
            postData['data'] = data;
            this.$refs.toolLabelIframe.contentWindow.postMessage(postData,'*');
          }
        },

        _receiveMessage(messageEvent) {
          if(messageEvent.data != undefined && messageEvent.data.key == 'iframeMessage') {
            let strJsonData = messageEvent.data.data;
            if(!this.isToolWebShow) {
              return;
            }
            GetQtController().ToQtCommand(strJsonData);
          }
        }
      },
    }
</script>

<style scoped>
  .IframStyle{
    width: 1920px;
    height: 1080px;
    position: absolute;
    top: 0px;
    left: 0px;
    z-index: 999;
  }
</style>
